------------------------------------------------------------------------------------------------------------------------
      name:  <unnamed>
       log:  /Users/gxzheng/Desktop/fintech/socialreturn/social_return.log
  log type:  text
 opened on:  16 May 2023, 17:43:20

. 
. 
. ********************************
. **# Financial Fraction of Growth
. ********************************
. 
. 
. ** Method 1 - # of financial patents/# of total patents - by grant year
. 
. * all pats
. 
. use inputdata/all_patent_new.dta, clear 

. keep if app_year >= 2000 & app_year <= 2018
(473,942 observations deleted)

. 
. by grant_year wku, sort: gen nvals = _n ==1

. by grant_year: replace nvals = sum(nvals)
(4368303 real changes made)

. by grant_year: replace nvals = nvals[_N]
(4368303 real changes made)

. rename nvals count_tot_pat

. 
. collapse (max) count_tot_pat, by(grant_year)

. keep if grant_year >= 2000 & grant_year <= 2018
(2 observations deleted)

. rename grant_year year

. 
. tempfile count_tot_pat

. save `count_tot_pat'
file /var/folders/y0/7tm8swhj7q9bm8t8f8q4g_gr0000gq/T//S_52133.000002 saved as .dta format

. 
. 
. 
. * fin pats  (use fin_addition)
. 
. use inputdata/fin_addition, clear

. 
. // grant year range
. tostring issue_date, replace
issue_date was long now str8

. gen issue_year = substr(issue_date, 1, 4)

. destring issue_year, replace
issue_year: all characters numeric; replaced as int
(458 missing values generated)

. keep if issue_year >= 2000 & issue_year <= 2018
(92,521 observations deleted)

. 
. 
. 
. // fin firm fin pats
. preserve

. keep if xrd !=. & fin == 1
(1,105,641 observations deleted)

. collapse (count) patent_id, by(issue_year)

. rename issue_year year

. rename patent_id count_fin_pat_gy_finfirm

. merge 1:1 year using `count_tot_pat'

    Result                      Number of obs
    -----------------------------------------
    Not matched                             1
        from master                         0  (_merge==1)
        from using                          1  (_merge==2)

    Matched                                18  (_merge==3)
    -----------------------------------------

. drop _m

. save fin_fraction.dta, replace
file fin_fraction.dta saved

. restore

. 
. 
. // fin firm nonfin pats 
. preserve

. keep if fin == 0 & xrd != .
(87,523 observations deleted)

. by issue_year patent_id, sort: gen nvals = _n ==1

. by issue_year: replace nvals = sum(nvals)
(1023082 real changes made)

. by issue_year: replace nvals = nvals[_N]
(1023082 real changes made)

. rename nvals count_nonfin_pat_gy_finfirm

. 
. collapse (max) count_nonfin_pat_gy_finfirm, by(issue_year)

. rename issue_year year

. merge 1:1 year using fin_fraction.dta

    Result                      Number of obs
    -----------------------------------------
    Not matched                             0
    Matched                                19  (_merge==3)
    -----------------------------------------

. drop _m

. save fin_fraction.dta, replace
file fin_fraction.dta saved

. restore

. 
. 
. * all nonfin pats  (use all_pat - v3)
. 
. use inputdata/all_patent_new.dta, clear

. rename wku patent_id

. merge 1:1 patent_id using inputdata/financial_patent_data_v3.dta, keepusing(public)
(variable patent_id was long, now double to accommodate using data's values)

    Result                      Number of obs
    -----------------------------------------
    Not matched                     4,818,011
        from master                 4,818,011  (_merge==1)
        from using                          0  (_merge==2)

    Matched                            24,255  (_merge==3)
    -----------------------------------------

. gen fin = cond(_m==3,1,0)

. drop _m

. 
. keep if fin == 0
(24,255 observations deleted)

. bys grant_year: egen count_nonfin_pat_gy_all = nvals(patent_id)

. 
. collapse (max) count_nonfin_pat_gy_all, by(grant_year)

. rename grant_year year

. merge 1:1 year using fin_fraction.dta

    Result                      Number of obs
    -----------------------------------------
    Not matched                             2
        from master                         2  (_merge==1)
        from using                          0  (_merge==2)

    Matched                                19  (_merge==3)
    -----------------------------------------

. drop _m

. save fin_fraction.dta, replace
file fin_fraction.dta saved

. 
. 
. 
. 
. 
. 
. ** Method 2 - mean adjusted - citations of fin/total citations of all
. 
. * all pats
. use inputdata/all_patent_new.dta, clear 

. keep if app_year >= 2000 & app_year <= 2018
(473,942 observations deleted)

. 
. collapse (sum) weighted_cit, by(grant_year)

. rename weighted_cit weighted_cite_tot

. 
. keep if grant_year >= 2000 & grant_year <= 2018
(2 observations deleted)

. rename grant_year year

. 
. merge 1:1 year using fin_fraction.dta

    Result                      Number of obs
    -----------------------------------------
    Not matched                             2
        from master                         0  (_merge==1)
        from using                          2  (_merge==2)

    Matched                                19  (_merge==3)
    -----------------------------------------

. drop _m

. save fin_fraction.dta, replace
file fin_fraction.dta saved

. 
. 
. 
. * fin pats 
. 
. // fin pats from fin firms, weighted cite from v3
. use inputdata/fin_addition.dta, clear

. merge m:1 patent_id using inputdata/financial_patent_data_v3.dta, keepusing(weighted_cite)

    Result                      Number of obs
    -----------------------------------------
    Not matched                     1,215,345
        from master                 1,197,020  (_merge==1)
        from using                     18,325  (_merge==2)

    Matched                             6,125  (_merge==3)
    -----------------------------------------

. replace weighted_cite = 0 if weighted_cite == .
(1,197,023 real changes made)

. drop if _m == 2
(18,325 observations deleted)

. drop _m

. 
. 
. tostring issue_date, replace
issue_date was long now str8

. gen issue_year = substr(issue_date, 1, 4)

. destring issue_year, replace
issue_year: all characters numeric; replaced as int
(458 missing values generated)

. keep if issue_year >= 2000 & issue_year <= 2018
(92,521 observations deleted)

. 
. 
. keep if fin == 1 & xrd != .
(1,105,641 observations deleted)

. collapse (sum) weighted_cite, by(issue_year)

. rename weighted_cite weighted_cite_fin_finfirm

. rename issue_year year

. merge 1:1 year using fin_fraction.dta

    Result                      Number of obs
    -----------------------------------------
    Not matched                             3
        from master                         0  (_merge==1)
        from using                          3  (_merge==2)

    Matched                                18  (_merge==3)
    -----------------------------------------

. drop _m

. save fin_fraction.dta, replace
file fin_fraction.dta saved

. 
. 
. 
. 
. // nonfin pats from fin firms, weighted cite from all_pat
. use inputdata/fin_addition.dta, clear

. rename patent_id wku

. merge m:1 wku using inputdata/all_patent_new.dta, keepusing(weighted_cit grant_year)

    Result                      Number of obs
    -----------------------------------------
    Not matched                     3,658,293
        from master                       458  (_merge==1)
        from using                  3,657,835  (_merge==2)

    Matched                         1,202,687  (_merge==3)
    -----------------------------------------

. drop _m

. 
. keep if fin == 0 & xrd != .
(3,754,049 observations deleted)

. collapse (sum) weighted_cit, by(grant_year)

. keep if grant_year >= 2000 & grant_year <= 2018
(2 observations deleted)

. 
. rename grant_year year

. rename weighted_cit weighted_cite_nonfin_finfirm

. 
. merge 1:1 year using fin_fraction.dta

    Result                      Number of obs
    -----------------------------------------
    Not matched                             2
        from master                         0  (_merge==1)
        from using                          2  (_merge==2)

    Matched                                19  (_merge==3)
    -----------------------------------------

. drop _m

. save fin_fraction.dta, replace
file fin_fraction.dta saved

. 
. 
. 
. // get all firm's r&d from ccm_full
. 
. use inputdata/ccm_full.dta, clear

. keep LPERMCO fyear xrd

. duplicates drop

Duplicates in terms of all variables

(1,456 observations deleted)

. bysort LPERMCO fyear: gen n=_N

. tab n

          n |      Freq.     Percent        Cum.
------------+-----------------------------------
          1 |    124,109       99.98       99.98
          2 |         28        0.02      100.00
------------+-----------------------------------
      Total |    124,137      100.00

. drop if n==2
(28 observations deleted)

. drop n

. // collapse (mean) xrd, by(LPERMCO fyear)
. rename LPERMCO permco

. rename fyear app_year

. save permco_rd_all.dta, replace
file permco_rd_all.dta saved

. 
. 
. 
. // all firm's nonfin pats  (use all_new - v3), weighted cite from all_pat
. 
. use inputdata/all_patent_new.dta, clear

. rename wku patent_id

. merge 1:1 patent_id using inputdata/financial_patent_data_v3.dta, keepusing(weighted_cite)
(variable patent_id was long, now double to accommodate using data's values)

    Result                      Number of obs
    -----------------------------------------
    Not matched                     4,818,011
        from master                 4,818,011  (_merge==1)
        from using                          0  (_merge==2)

    Matched                            24,255  (_merge==3)
    -----------------------------------------

. gen fin = cond(_m==3,1,0)

. drop _m

. 
. merge 1:m patent_id using inputdata/public_patent_permco_2019.dta, keepusing(permco)

    Result                      Number of obs
    -----------------------------------------
    Not matched                     4,543,589
        from master                 3,217,775  (_merge==1)
        from using                  1,325,814  (_merge==2)

    Matched                         1,624,491  (_merge==3)
    -----------------------------------------

. drop if _m == 2
(1,325,814 observations deleted)

. drop _m

. 
. merge m:1 permco app_year using permco_rd_all.dta, keepusing(xrd)
(variable permco was long, now double to accommodate using data's values)
(variable app_year was int, now double to accommodate using data's values)

    Result                      Number of obs
    -----------------------------------------
    Not matched                     3,508,549
        from master                 3,408,506  (_merge==1)
        from using                    100,043  (_merge==2)

    Matched                         1,433,760  (_merge==3)
    -----------------------------------------

. drop if _m == 2
(100,043 observations deleted)

. drop _m

. 
. bysort permco: egen sumrd = sum( xrd )

. keep if sumrd !=0
(3,265,153 observations deleted)

. 
. keep if fin == 0 
(5,357 observations deleted)

. rename weighted_cit weighted_cite_nonfin_all

. collapse (sum) weighted_cite_nonfin_all, by(grant_year)

. 
. rename grant_year year

. merge 1:1 year using fin_fraction.dta

    Result                      Number of obs
    -----------------------------------------
    Not matched                             1
        from master                         0  (_merge==1)
        from using                          1  (_merge==2)

    Matched                                20  (_merge==3)
    -----------------------------------------

. drop _m

. save fin_fraction.dta, replace
file fin_fraction.dta saved

. 
. 
. 
. 
. // all firm's fin pats (use all_new - v3), weighted cite from v3
. 
. use inputdata/all_patent_new.dta, clear

. rename wku patent_id

. merge 1:1 patent_id using inputdata/financial_patent_data_v3.dta, keepusing(weighted_cite)
(variable patent_id was long, now double to accommodate using data's values)

    Result                      Number of obs
    -----------------------------------------
    Not matched                     4,818,011
        from master                 4,818,011  (_merge==1)
        from using                          0  (_merge==2)

    Matched                            24,255  (_merge==3)
    -----------------------------------------

. gen fin = cond(_m==3,1,0)

. drop _m

. 
. merge 1:1 patent_id using inputdata/public_patent_permco_2019.dta, keepusing(permco)

    Result                      Number of obs
    -----------------------------------------
    Not matched                     4,543,589
        from master                 3,217,775  (_merge==1)
        from using                  1,325,814  (_merge==2)

    Matched                         1,624,491  (_merge==3)
    -----------------------------------------

. drop if _m == 2
(1,325,814 observations deleted)

. drop _m

. 
. merge m:1 permco app_year using permco_rd_all.dta, keepusing(xrd)
(variable permco was long, now double to accommodate using data's values)
(variable app_year was int, now double to accommodate using data's values)

    Result                      Number of obs
    -----------------------------------------
    Not matched                     3,508,549
        from master                 3,408,506  (_merge==1)
        from using                    100,043  (_merge==2)

    Matched                         1,433,760  (_merge==3)
    -----------------------------------------

. drop if _m == 2
(100,043 observations deleted)

. drop _m

. 
. bysort permco: egen sumrd = sum( xrd )

. keep if sumrd !=0
(3,265,153 observations deleted)

. 
. keep if fin == 1 & xrd != .
(1,572,094 observations deleted)

. rename weighted_cite weighted_cite_fin_all

. collapse (sum) weighted_cite_fin_all, by(grant_year)

. 
. rename grant_year year

. merge 1:1 year using fin_fraction.dta

    Result                      Number of obs
    -----------------------------------------
    Not matched                             2
        from master                         0  (_merge==1)
        from using                          2  (_merge==2)

    Matched                                19  (_merge==3)
    -----------------------------------------

. drop _m

. save fin_fraction.dta, replace
file fin_fraction.dta saved

. 
. 
. 
. // all fin pats for FED cost (use v3)
. use inputdata/financial_patent_data_v3.dta, clear

. gen grant_year = year(grant_date)

. 
. preserve

. collapse (sum) weighted_cite, by(grant_year)

. rename weighted_cite weighted_cite_fin_gy_v3

. rename grant_year year

. 
. merge 1:1 year using fin_fraction.dta

    Result                      Number of obs
    -----------------------------------------
    Not matched                             2
        from master                         0  (_merge==1)
        from using                          2  (_merge==2)

    Matched                                19  (_merge==3)
    -----------------------------------------

. drop _m

. save fin_fraction.dta, replace
file fin_fraction.dta saved

. restore

. 
. 
. 
. // excluding IT
. preserve

. 
. gen bank=(primary_industry=="Diversified Banks" | primary_industry=="Regional Banks" | primary_industry=="Thrifts and 
> Mortgage Finance")

. gen capmkt=(primary_industry=="Asset Management and Custody Banks" | primary_industry=="Diversified Capital Markets" |
>  primary_industry=="Diversified REITs" | primary_industry=="Financial Exchanges and Data" | primary_industry=="Investm
> ent Banking and Brokerage" | primary_industry=="Specialized REITs") 

. gen consfin=(primary_industry=="Consumer Finance" | primary_industry=="Specialized Finance")

. gen insur=(primary_industry=="Life and Health Insurance" | primary_industry=="Multi-line Insurance" | primary_industry
> =="Property and Casualty Insurance" | primary_industry=="Reinsurance")

. gen pay=(primary_industry=="Data Processing and Outsourced Services")

. gen it=(primary_industry=="Application Software" | primary_industry=="Communications Equipment" | primary_industry=="C
> onsumer Electronics" | primary_industry=="Electrical Components and Equipment" | primary_industry=="Electronic Compone
> nts")

. replace it=1 if (primary_industry=="IT Consulting and Other Services" | primary_industry=="Electronic Equipment and In
> struments" | primary_industry=="Electronic Manufacturing Services" | primary_industry=="Integrated Telecommunication S
> ervices" | primary_industry=="Interactive Media and Services")
(1,967 real changes made)

. replace it=1 if (primary_industry=="Internet Services and Infrastructure" | primary_industry=="Internet and Direct Mar
> keting Retail" | primary_industry=="Semiconductor Equipment" | primary_industry=="Semiconductors" | primary_industry==
> "Specialized Consumer Services" | primary_industry=="Systems Software")
(1,862 real changes made)

. replace it=1 if (primary_industry=="Technology Distributors" | primary_industry=="Technology Hardware, Storage and Per
> ipherals" | primary_industry=="Wireless Telecommunication Services")
(1,883 real changes made)

. gen otherind=(bank==0 & capmkt==0 & consfin==0 & insur==0 & it==0)

. gen industrytype=1 if bank==1
(22,654 missing values generated)

. replace industrytype=2 if capmkt==1
(1,618 real changes made)

. replace industrytype=3 if consfin==1
(715 real changes made)

. replace industrytype=4 if insur==1
(1,098 real changes made)

. replace industrytype=5 if pay==1
(2,390 real changes made)

. replace industrytype=6 if it==1
(9,057 real changes made)

. replace industrytype=7 if industrytype==.
(7,776 real changes made)

. label define industrytype1 1 "bank" 2 "capmkt" 3 "consfin" 4 "insur" 5 "payments" 6 "it" 7 "otherind" 

. label values industrytype industrytype1

. gen indsimpletype=1 if bank==1
(22,654 missing values generated)

. replace indsimpletype=2 if (capmkt==1 | consfin==1 | insur==1)
(3,431 real changes made)

. replace indsimpletype=5 if pay==1
(2,390 real changes made)

. replace indsimpletype=6 if (it==1 | industrytype==7)
(16,833 real changes made)

. label define indsimpletype1 1 "Bank" 2 "Other Finance" 5 "Payments" 6 "IT and Other" 

. label values indsimpletype indsimpletype1

. 
. drop if indsimpletype == 6
(16,833 observations deleted)

. 
. 
. collapse (sum) weighted_cite, by(grant_year)

. rename weighted_cite weighted_cite_fin_gy_v3noit

. rename grant_year year

. 
. merge 1:1 year using fin_fraction.dta

    Result                      Number of obs
    -----------------------------------------
    Not matched                             2
        from master                         0  (_merge==1)
        from using                          2  (_merge==2)

    Matched                                19  (_merge==3)
    -----------------------------------------

. drop _m

. save fin_fraction.dta, replace
file fin_fraction.dta saved

. 
. restore

. 
. 
. 
. 
. 
. 
. 
. 
. 
. 
. 
. 
. 
. 
. ******************************
. **# R&D Expense in Fin patents
. ******************************
. 
. * use fin_addition (fin firm only)
. 
. * firm level fin fraction
. use inputdata/fin_addition.dta, clear

. keep patent_id app_year permco fin xrd xi_real

. 
. // by counts
. by app_year permco patent_id, sort: gen nvals = _n == 1 

. by app_year permco: replace nvals = sum(nvals)
(1198548 real changes made)

. by app_year permco: replace nvals = nvals[_N] 
(1198548 real changes made)

. rename nvals n_pats

. 
. by app_year permco patent_id, sort: gen nvals = _n == 1 if fin == 1
(1,197,776 missing values generated)

. by app_year permco: replace nvals = sum(nvals)
(1201947 real changes made)

. by app_year permco: replace nvals = nvals[_N] 
(538516 real changes made)

. rename nvals n_pats_fin

. 
. by app_year permco patent_id, sort: gen nvals = _n == 1 if fin == 0
(5,369 missing values generated)

. by app_year permco: replace nvals = sum(nvals)
(1198601 real changes made)

. by app_year permco: replace nvals = nvals[_N] 
(1198368 real changes made)

. rename nvals n_pats_nonfin

. 
. bys app_year permco: gen firm_fin_frac_count = n_pats_fin/n_pats

. bys app_year permco: gen firm_nonfin_frac_count = n_pats_nonfin/n_pats

. 
. 
. 
. // by citations
. rename patent_id wku

. merge m:1 wku using inputdata/all_patent_new.dta, keepusing(weighted_cit)

    Result                      Number of obs
    -----------------------------------------
    Not matched                     3,658,293
        from master                       458  (_merge==1)
        from using                  3,657,835  (_merge==2)

    Matched                         1,202,687  (_merge==3)
    -----------------------------------------

. drop if _m == 2
(3,657,835 observations deleted)

. drop _m

. rename weighted_cit weighted_cite

. replace weighted_cite = 0 if weighted_cite == .
(580 real changes made)

. 
. 
. bys app_year permco: egen weighted_cite_fin = total(weighted_cite) if fin == 1
(1,197,776 missing values generated)

. bys app_year permco (weighted_cite_fin): replace weighted_cite_fin = weighted_cite_fin[_n-1] if missing(weighted_cite_
> fin)
(685635 real changes made)

. replace weighted_cite_fin = 0 if weighted_cite_fin == .
(512,141 real changes made)

. 
. bys app_year permco: egen weighted_cite_nonfin = total(weighted_cite) if fin == 0
(5,369 missing values generated)

. bys app_year permco (weighted_cite_nonfin): replace weighted_cite_nonfin = weighted_cite_nonfin[_n-1] if missing(weigh
> ted_cite_nonfin)
(5302 real changes made)

. replace weighted_cite_nonfin = 0 if weighted_cite_nonfin == .
(67 real changes made)

. 
. bys app_year permco: egen weighted_cite_tot = total(weighted_cite)

. replace weighted_cite_tot = 0 if weighted_cite_tot == .
(0 real changes made)

. 
. bys app_year permco: gen firm_fin_frac_cite = weighted_cite_fin/weighted_cite_tot
(978 missing values generated)

. bys app_year permco: gen firm_nonfin_frac_cite = weighted_cite_nonfin/weighted_cite_tot
(978 missing values generated)

. 
. 
. 
. * fin rd
. keep if xrd != .
(91,191 observations deleted)

. gen rd_fin_count = xrd * firm_fin_frac_count

. gen rd_fin_cite = xrd * firm_fin_frac_cite
(709 missing values generated)

. 
. gen rd_nonfin_count = xrd * firm_nonfin_frac_count

. gen rd_nonfin_cite = xrd * firm_nonfin_frac_cite
(709 missing values generated)

. 
. // need to collpase into firm level and then aggregate, 
. // or else the aggregate will be repetitive with many patents
. collapse (max) firm_fin_frac_count firm_fin_frac_cite firm_nonfin_frac_count firm_nonfin_frac_cite rd_fin_count rd_fin
> _cite rd_nonfin_count rd_nonfin_cite, by(permco app_year)

. 
. gen firm_ever_fin_rd = cond(rd_fin_count != 0 , 1, 0)

. 
. collapse (sum) rd_fin_count rd_fin_cite rd_nonfin_count rd_nonfin_cite firm_ever_fin_rd, by(app_year)

. 
. rename firm_ever_fin_rd num_firm_fin_rd

. rename app_year year

. 
. merge 1:1 year using fin_fraction.dta

    Result                      Number of obs
    -----------------------------------------
    Not matched                             2
        from master                         0  (_merge==1)
        from using                          2  (_merge==2)

    Matched                                19  (_merge==3)
    -----------------------------------------

. drop _m

. save fin_fraction.dta, replace
file fin_fraction.dta saved

. 
. 
. 
. 
. ****************************
. ** private return kpss/rd **
. ****************************
. 
. * distinguish nonfin by fin innovators
. use inputdata/fin_addition.dta, clear

. keep patent_id app_year permco fin xrd xi_real

. 
. gen kpss_fin = xi_real if fin == 1
(1,197,776 missing values generated)

. replace kpss_fin = 0 if kpss_fin == .
(1,197,776 real changes made)

. 
. gen kpss_nonfin = xi_real if fin == 0
(161,068 missing values generated)

. replace kpss_nonfin = 0 if kpss_nonfin == .
(161,068 real changes made)

. 
. collapse (max) kpss_fin kpss_nonfin, by(permco app_year)

. collapse (sum) kpss_fin kpss_nonfin, by(app_year)

. 
. rename app_year year

. merge 1:1 year using fin_fraction.dta

    Result                      Number of obs
    -----------------------------------------
    Not matched                             2
        from master                         0  (_merge==1)
        from using                          2  (_merge==2)

    Matched                                19  (_merge==3)
    -----------------------------------------

. drop _m

. 
. gen kpssrd_fin_count = kpss_fin/rd_fin_count
(2 missing values generated)

. gen kpssrd_fin_cite = kpss_fin/rd_fin_cite
(2 missing values generated)

. 
. gen kpssrd_nonfin_count = kpss_nonfin/rd_nonfin_count
(2 missing values generated)

. gen kpssrd_nonfin_cite = kpss_nonfin/rd_nonfin_cite
(2 missing values generated)

. 
. save fin_fraction.dta, replace
file fin_fraction.dta saved

. 
. 
. 
. 
. * all nonfin pats among all innovators ---- all_new - v3
. use inputdata/all_patent_new.dta, clear

. keep if app_year >= 2000 & app_year <= 2018
(473,942 observations deleted)

. rename wku patent_id

. merge 1:1 patent_id using inputdata/financial_patent_data_v3.dta, keepusing(public)
(variable patent_id was long, now double to accommodate using data's values)

    Result                      Number of obs
    -----------------------------------------
    Not matched                     4,344,069
        from master                 4,344,069  (_merge==1)
        from using                          0  (_merge==2)

    Matched                            24,255  (_merge==3)
    -----------------------------------------

. gen fin_allpat = cond(_m==3|_m==2, 1, 0)

. drop _m

. 
. 
. * merge with permco, rd, and kpss
. 
. rename patent_id patent_num

. merge 1:1 patent_num using inputdata/public_patval_permco_2019.dta, keepusing(permco xi_real)

    Result                      Number of obs
    -----------------------------------------
    Not matched                     4,388,361
        from master                 2,903,190  (_merge==1)
        from using                  1,485,171  (_merge==2)

    Matched                         1,465,134  (_merge==3)
    -----------------------------------------

. drop if _m == 2
(1,485,171 observations deleted)

. drop _m

. rename patent_num patent_id

. 
. merge m:1 permco app_year using permco_rd_all.dta, keepusing(xrd)
(variable permco was long, now double to accommodate using data's values)
(variable app_year was int, now double to accommodate using data's values)

    Result                      Number of obs
    -----------------------------------------
    Not matched                     3,036,374
        from master                 2,936,075  (_merge==1)
        from using                    100,299  (_merge==2)

    Matched                         1,432,249  (_merge==3)
    -----------------------------------------

. drop if _m == 2
(100,299 observations deleted)

. drop _m

. 
. keep if xrd != .
(2,983,654 observations deleted)

. // bysort permco: egen sumrd = sum( xrd )
. // keep if sumrd !=0
. // replace xrd = 0 if xrd == .
. 
. 
. gen kpss_fin_all = xi_real if fin_allpat == 1
(1,379,541 missing values generated)

. replace kpss_fin_all = 0 if kpss_fin_all == .
(1,379,541 real changes made)

. 
. gen kpss_nonfin_all = xi_real if fin_allpat == 0
(5,129 missing values generated)

. replace kpss_nonfin_all = 0 if kpss_nonfin_all == .
(5,129 real changes made)

. 
. preserve

. collapse (max) kpss_fin_all kpss_nonfin_all, by(permco app_year)

. collapse (sum) kpss_fin_all kpss_nonfin_all, by(app_year)

. rename app_year year

. merge 1:1 year using fin_fraction.dta

    Result                      Number of obs
    -----------------------------------------
    Not matched                             2
        from master                         0  (_merge==1)
        from using                          2  (_merge==2)

    Matched                                19  (_merge==3)
    -----------------------------------------

. drop _m

. save fin_fraction.dta, replace
file fin_fraction.dta saved

. restore

. 
. 
. by app_year permco patent_id, sort: gen nvals = _n == 1 

. by app_year permco: replace nvals = sum(nvals)
(1364105 real changes made)

. by app_year permco: replace nvals = nvals[_N] 
(1364105 real changes made)

. rename nvals n_allpats

. 
. by app_year permco patent_id, sort: gen nvals = _n == 1 if fin == 1
(1,379,541 missing values generated)

. by app_year permco: replace nvals = sum(nvals)
(1383564 real changes made)

. by app_year permco: replace nvals = nvals[_N] 
(523899 real changes made)

. rename nvals n_allpats_fin

. 
. by app_year permco patent_id, sort: gen nvals = _n == 1 if fin == 0
(5,129 missing values generated)

. by app_year permco: replace nvals = sum(nvals)
(1364154 real changes made)

. by app_year permco: replace nvals = nvals[_N] 
(1363938 real changes made)

. rename nvals n_allpats_nonfin

. 
. bys app_year permco: gen firm_fin_frac = n_allpats_fin/n_allpats

. bys app_year permco: gen firm_nonfin_frac = n_allpats_nonfin/n_allpats

. 
. bys app_year permco: gen rd_fin_all = xrd * firm_fin_frac

. bys app_year permco: gen rd_nonfin_all = xrd * firm_nonfin_frac

. bys app_year permco: gen rd_all = xrd

. 
. preserve

. collapse (max) rd_fin_all rd_nonfin_all rd_all, by(app_year permco)

. collapse (sum) rd_fin_all rd_nonfin_all rd_all, by(app_year)

. rename app_year year

. merge 1:1 year using fin_fraction.dta

    Result                      Number of obs
    -----------------------------------------
    Not matched                             2
        from master                         0  (_merge==1)
        from using                          2  (_merge==2)

    Matched                                19  (_merge==3)
    -----------------------------------------

. drop _m

. save fin_fraction.dta, replace
file fin_fraction.dta saved

. restore

. 
. 
. use fin_fraction.dta, clear

. gen kpssrd_fin_all = kpss_fin_all / rd_fin_all
(2 missing values generated)

. gen kpssrd_nonfin_all = kpss_nonfin_all / rd_nonfin_all
(2 missing values generated)

. 
. save fin_fraction.dta, replace
file fin_fraction.dta saved

. 
. 
. 
. 
. 
. 
. 
. ************************
. **# GDP and TFP growth *
. ************************
. 
. 
. * use Real GDP and TFP data from St.Louis Fed (based on 2017)
. clear

. set fredkey c36e677d78016be9acccb6b14b90be72

. import fred RGDPNAUSA666NRUG RTFPNAUSA632NRUG BOGZ1FA615013043A BOGZ1FA795013045A BOGZ1FA885013045A

Summary
------------------------------------------------------------------------------------------------------------------------
Series ID                    Nobs    Date range                Frequency
------------------------------------------------------------------------------------------------------------------------
RGDPNAUSA666NRUG             70      1950-01-01 to 2019-01-01  Annual
RTFPNAUSA632NRUG             66      1954-01-01 to 2019-01-01  Annual
BOGZ1FA615013043A            77      1946-01-01 to 2022-01-01  Annual
BOGZ1FA795013045A            77      1946-01-01 to 2022-01-01  Annual
BOGZ1FA885013045A            77      1946-01-01 to 2022-01-01  Annual
------------------------------------------------------------------------------------------------------------------------
# of series imported: 5
   highest frequency: Annual
    lowest frequency: Annual

. rename RGDPNAUSA666NRUG real_gdp //Real GDP at Constant National Prices for United States 

. rename RTFPNAUSA632NRUG tfp_index // Total Factor Productivity at Constant National Prices for United States

. rename BOGZ1FA795013045A invest_fin_sec // Domestic Financial Sectors; Gross Fixed Investment, Nonresidential Research
>  and Development, Transactions

. rename BOGZ1FA885013045A invest_all_sec // All Domestic Sectors; Gross Fixed Investment, Nonresidential Research and D
> evelopment, Transactions

. 
. 
. 
. gen year = year(daten)

. keep if year >= 1999 & year <= 2019
(56 observations deleted)

. keep year real_gdp tfp_index invest_fin_sec invest_all_sec

. 
. gen invest_nonfin_sec = invest_all_sec - invest_fin_sec

. 
. * adjust to 2019 price, since rd is calculated in 2019
. merge 1:1 year using inputdata/cpi.dta

    Result                      Number of obs
    -----------------------------------------
    Not matched                            86
        from master                         0  (_merge==1)
        from using                         86  (_merge==2)

    Matched                                21  (_merge==3)
    -----------------------------------------

. keep if _m == 3
(86 observations deleted)

. drop _m

. // real gdp in 2019 is 263.293 million usd
. gen real_gdp_adj = real_gdp * (cpi / cpi[21])

. gen tfp_index_adj = tfp_index * (cpi / cpi[21])

. 
. tsset year

Time variable: year, 1999 to 2019
        Delta: 1 unit

. gen tfp_adj_growth = D.tfp_index_adj / L.tfp_index_adj
(1 missing value generated)

. gen gdp_adj_growth = D.real_gdp_adj/ L.real_gdp_adj
(1 missing value generated)

. 
. egen long_run_tfp_growth = mean(tfp_adj_growth)

. egen long_run_gdp_adj_growth = mean(gdp_adj_growth)

. 
. keep if year >= 2000 & year <= 2018
(2 observations deleted)

. 
. save growth.dta, replace
file growth.dta saved

. 
. 
. 
. 
. 
. 
. ***********************************
. **# Social Return for Fin patents *
. ***********************************
. 
. use fin_fraction, clear

. merge 1:1 year using growth.dta

    Result                      Number of obs
    -----------------------------------------
    Not matched                             2
        from master                         2  (_merge==1)
        from using                          0  (_merge==2)

    Matched                                19  (_merge==3)
    -----------------------------------------

. drop _m

. 
. 
. 
. 
. ** Benefit **
. 
. * for fin tfp growth, use public with rd avail in fin_addition, forward one year
. gen frac_fin_count = count_fin_pat_gy_finfirm[_n+1] / count_tot_pat[_n+1]
(3 missing values generated)

. gen frac_fin_cite = weighted_cite_fin_finfirm[_n+1] / weighted_cite_tot[_n+1]
(3 missing values generated)

. gen frac_fin_v3_cite = weighted_cite_fin_gy_v3[_n+1] / weighted_cite_tot[_n+1]
(3 missing values generated)

. gen frac_fin_noit_cite = weighted_cite_fin_gy_v3noit[_n+1] / weighted_cite_tot[_n+1]
(3 missing values generated)

. gen frac_fin_all_cite = weighted_cite_fin_all[_n+1] / weighted_cite_tot[_n+1]
(3 missing values generated)

. 
. 
. gen fin_growth_count = frac_fin_count * tfp_adj_growth[_n+1]
(3 missing values generated)

. gen fin_growth_cite = frac_fin_cite * tfp_adj_growth[_n+1]
(3 missing values generated)

. gen fin_growth_v3_cite = frac_fin_v3_cite * tfp_adj_growth[_n+1]
(3 missing values generated)

. gen fin_growth_all_cite = frac_fin_all_cite * tfp_adj_growth[_n+1]
(3 missing values generated)

. gen fin_growth_noit_cite = frac_fin_noit_cite * tfp_adj_growth[_n+1]
(3 missing values generated)

. 
. 
. * for nonfin tfp growth, use firms with rd avail in fin_addition, forward one year
. gen frac_nonfin_count = count_nonfin_pat_gy_finfirm[_n+1] / count_tot_pat[_n+1]
(3 missing values generated)

. gen frac_nonfin_cite = weighted_cite_nonfin_finfirm[_n+1] / weighted_cite_tot[_n+1]
(3 missing values generated)

. gen frac_nonfin_all_cite = weighted_cite_nonfin_all[_n+1] / weighted_cite_tot[_n+1]
(3 missing values generated)

. 
. 
. gen nonfin_growth_count = frac_nonfin_count * tfp_adj_growth[_n+1]
(3 missing values generated)

. gen nonfin_growth_cite = frac_nonfin_cite * tfp_adj_growth[_n+1]
(3 missing values generated)

. gen nonfin_growth_all_cite = frac_nonfin_all_cite * tfp_adj_growth[_n+1]
(3 missing values generated)

. 
. 
. 
. 
. 
. 
. 
. 
. ** Cost **
. 
. * for fin rd share in gdp, we only have public firms pat fraction in fin
. // fin firm's fin
. gen fin_innoshare_count = rd_fin_count / real_gdp_adj
(2 missing values generated)

. 
. // FED data fin
. gen fin_innoshare_sec = invest_fin_sec / real_gdp_adj
(2 missing values generated)

. 
. // all firm's fin
. gen fin_innoshare_all = rd_fin_all / real_gdp_adj
(2 missing values generated)

. 
. * nonfin 
. // fin firm's nonfin
. gen nonfin_innoshare_count = rd_nonfin_count/real_gdp_adj
(2 missing values generated)

. 
. // all firm's nonfin
. gen nonfin_innoshare_all = rd_nonfin_all / real_gdp_adj
(2 missing values generated)

. 
. 
. 
. 
. 
. 
. ** Rho **
. 
. * set r = 0.05
. // financial social return rate
. 
. gen rho_fin_count_5pp = (fin_growth_count / 0.05) / fin_innoshare_count
(3 missing values generated)

. 
. gen rho_fin_cite_5pp = (fin_growth_cite / 0.05) / fin_innoshare_count //cost still use count
(3 missing values generated)

. 
. gen rho_fin_all_cite_5pp = (fin_growth_all_cite / 0.05) / fin_innoshare_all // all firms
(3 missing values generated)

. 
. gen rho_fin_sec_cite_5pp = (fin_growth_v3_cite / 0.05) / fin_innoshare_sec  // fed 
(3 missing values generated)

. 
. gen rho_fin_noit_cite_5pp = (fin_growth_noit_cite / 0.05) / fin_innoshare_sec //noit fin/fed
(3 missing values generated)

. 
. 
. // non-fin social return 
. 
. gen rho_nonfin_count_5pp = (nonfin_growth_count / 0.05) / nonfin_innoshare_count
(3 missing values generated)

. 
. gen rho_nonfin_cite_5pp = (nonfin_growth_cite / 0.05) / nonfin_innoshare_count //cost still use count
(3 missing values generated)

. 
. gen rho_nonfin_all_cite_5pp = (nonfin_growth_all_cite / 0.05) / nonfin_innoshare_all // all firms
(3 missing values generated)

. 
. 
. save social_return.dta, replace
file social_return.dta saved

. 
. 
. 
. 
. 
. 
. 
. 
. *****************************
. **# Final Summary and Plots *
. *****************************
. 
. use social_return.dta, clear

. 
. 
. 
. 
. ** Figure 7-A **
. * plot r=0.05, by citations, from fin firms
. preserve

. keep if year >= 2000 & year <= 2017
(3 observations deleted)

. twoway (line rho_fin_cite_5pp year, lwidth(medthick))  ///
>           (line rho_nonfin_cite_5pp year, lpattern(-)), ///
>           ytitle("Avg Social Return {&rho}", size(small)) xtitle("Year", size(medsmall)) ///
>           note("Note: Discount rate = 5%. Firm set includes only financial innovators." "Finance fraction of benefit i
> s calculated by weighted citations of granted finance patents." "Finance fraction of cost is calculated by filed finan
> ce patent counts.", size(vsmall)) ///
>           yline(0, lwidth(vthin) lpattern(.) lcolor(grey)) xlabel(#9, angle(45)) ///
>           legend(label(1 "Financial Innovation") label(2 "Non-financial Innovation") size(small) rows(1)) graphregion(
> fcolor(white)) ysc(titlegap(+2))
(note:  named style grey not found in class color, default attributes used)

. graph export "figure/Fig_7A_cite_fnf.png", as(png) replace
file /Users/gxzheng/Desktop/fintech/socialreturn/figure/Fig_7A_cite_fnf.png saved as PNG format

. restore

. 
. 
. 
. ** Figure 7-B **
. * plot fin-kpss/rd vs nonfin-kpss/rd, cost using count, among fin innovators
. preserve

. keep if year >=2000 & year <= 2017
(3 observations deleted)

. twoway (line kpssrd_fin_count year, lwidth(medthick) c(l) yaxis(1))  ///
>           (line  kpssrd_nonfin_count year, lpattern(-) c(l) yaxis(2)), ///
>           ytitle("Avg Fin Private Return (KPSS/R&D)", size(small) axis(1)) ///
>           ytitle("Avg Non-fin Private Return (KPSS/R&D)", size(small) axis(2)) ///
>           xtitle("Year", size(medsmall)) graphregion(fcolor(white)) ///
>           note("Note: R&D cost is fractioned by the counts of either finance or non-finance filed patents. Firm set in
> cludes only financial innovators.", size(vsmall)) ///
>           xlabel(#9, angle(45)) ysc(titlegap(+2)) ///
>           legend(label(1 "Financial Innovation") label(2 "Non-financial Innovation") size(small) rows(1)) 

. graph export "figure/Fig_7B_kpssrd_fnf_finfirm.png", as(png) replace
file /Users/gxzheng/Desktop/fintech/socialreturn/figure/Fig_7B_kpssrd_fnf_finfirm.png saved as PNG format

. restore

. 
. 
. 
. ** Figure A-12-A **
. * plot r=0.05, by counts
. preserve

. keep if year >=2000 & year <= 2017
(3 observations deleted)

. twoway (line rho_fin_count_5pp year, lwidth(medthick))  ///
>           (line rho_nonfin_count_5pp year, lpattern(-)), ///
>           ytitle("Avg Social Return {&rho}", size(small)) xtitle("Year", size(medsmall)) ///
>           note("Note: Discount rate = 5%. Firm set includes only financial innovators." "Finance fraction of benefit i
> s calculated by counts of granted finance patents, while the cost is calculated by counts of filed finance patents.", 
> size(vsmall)) ///
>           yline(0, lwidth(vthin) lpattern(.) lcolor(grey)) xlabel(#9, angle(45)) ///
>           legend(label(1 "Financial Innovation") label(2 "Non-financial Innovation") size(small) rows(1)) graphregion(
> fcolor(white)) ysc(titlegap(+2))
(note:  named style grey not found in class color, default attributes used)

. graph export "figure/Fig_A12A_count_fnf.png", as(png) replace
file /Users/gxzheng/Desktop/fintech/socialreturn/figure/Fig_A12A_count_fnf.png saved as PNG format

. restore

. 
. 
. 
. ** Figure A-12-B **
. * plot r=0.05, by citations, from all firms
. preserve

. keep if year >= 2000 & year <= 2017
(3 observations deleted)

. twoway (line rho_fin_all_cite_5pp year, lwidth(medthick))  ///
>           (line rho_nonfin_all_cite_5pp year, lpattern(-)), ///
>           ytitle("Avg Social Return {&rho}", size(small)) xtitle("Year", size(medsmall)) ///
>           note("Note: Discount rate = 5%. Firm set includes all R&D available innovators." "Finance fraction of benefi
> t is calculated by weighted citations of granted finance patents." "Finance fraction of cost is calculated by filed fi
> nance patent counts.", size(vsmall)) ///
>           yline(0, lwidth(vthin) lpattern(.) lcolor(grey)) xlabel(#9, angle(45)) ///
>           legend(label(1 "Financial Innovation") label(2 "Non-financial Innovation") size(small) rows(1)) graphregion(
> fcolor(white)) ysc(titlegap(+2))
(note:  named style grey not found in class color, default attributes used)

. graph export "figure/Fig_A12B_cite_fnf_all.png", as(png) replace
file /Users/gxzheng/Desktop/fintech/socialreturn/figure/Fig_A12B_cite_fnf_all.png saved as PNG format

. restore

. 
. 
. 
. ** Figure A-12-C **
. * plot fin-kpss/rd vs nonfin-kpss/rd, cost using count, among all innovators
. preserve

. keep if year >=2000 & year <= 2017
(3 observations deleted)

. twoway (line kpssrd_fin_all year, lwidth(medthick) c(l) yaxis(1)) ///
>                 (line kpssrd_nonfin_all year, lpattern(-) c(l) yaxis(2)), ///
>           ytitle("Avg Fin Private Return (KPSS/R&D)", size(small) axis(1)) ///
>           ytitle("Avg Non-fin Private Return (KPSS/R&D)", size(small) axis(2)) ///
>           xtitle("Year", size(medsmall)) graphregion(fcolor(white)) ///
>           note("Note: R&D cost is fractioned by the counts of either finance or non-finance filed patents. Firm set in
> cludes all R&D available innovators.", size(vsmall)) ///
>           xlabel(#9, angle(45)) ysc(titlegap(+2)) ///
>           legend(label(1 "Financial Innovation") label(2 "Non-financial Innovation") size(small) rows(1)) 

. graph export "figure/Fig_A12C_kpssrd_fnf_allfirm.png", as(png) replace
file /Users/gxzheng/Desktop/fintech/socialreturn/figure/Fig_A12C_kpssrd_fnf_allfirm.png saved as PNG format

. restore

. 
. 
. 
. ** Figure A-13-A **
. 
. * plot r=0.05, fin by cite VS fed ratio
. preserve

. keep if year >=2000 & year <= 2017
(3 observations deleted)

. twoway (line rho_fin_cite_5pp year, lwidth(medthick))  ///
>           (line  rho_fin_sec_cite_5pp year, lpattern(-)), ///
>           ytitle("Avg Financial Social Return {&rho}", size(small)) xtitle("Year", size(medsmall)) ///
>           note("Note: Discount rate = 5%. Finance fraction of benefits are both calculated by weighted citations of gr
> anted patents." "Cost using R&D micro data is proportioned by filed finance patent counts. Cost using Fed macro data i
> s the financial sector R&D.", size(vsmall)) ///
>           yline(0, lwidth(vthin) lpattern(.) lcolor(grey)) xlabel(#9, angle(45)) ///
>           legend(label(1 "Cost using R&D micro data") label(2 "Cost using FED macro data") size(small) rows(1)) graphr
> egion(fcolor(white)) ysc(titlegap(+2))
(note:  named style grey not found in class color, default attributes used)

. graph export "figure/Fig_A13A_cite+fed.png", as(png) replace
file /Users/gxzheng/Desktop/fintech/socialreturn/figure/Fig_A13A_cite+fed.png saved as PNG format

. restore

. 
. 
. 
. 
. ** Figure A-13-B **
. * plot r=0.05, fin by cite VS fed noit 
. preserve

. keep if year >=2000 & year <= 2017
(3 observations deleted)

. twoway (line rho_fin_cite_5pp year, lwidth(medthick))  ///
>           (line  rho_fin_noit_cite_5pp year, lpattern(-)), ///
>           ytitle("Avg Financial Social Return {&rho}", size(small)) xtitle("Year", size(medsmall)) ///
>           note("Note: Discount rate = 5%. Finance fraction of benefits are both calculated by weighted citations of gr
> anted patents." "Cost using R&D micro data is proportioned by filed finance patent counts. Cost using Fed macro data i
> s the financial sector R&D.", size(vsmall)) ///
>           yline(0, lwidth(vthin) lpattern(.) lcolor(grey)) xlabel(#9, angle(45)) ///
>           legend(label(1 "Cost using R&D micro data") label(2 "Cost using FED macro data; Benefit excluding IT Finanac
> ial Patents") size(small) rows(2)) graphregion(fcolor(white)) ysc(titlegap(+2))
(note:  named style grey not found in class color, default attributes used)

. graph export "figure/Fig_A13B_cite+fednoit.png", as(png) replace
file /Users/gxzheng/Desktop/fintech/socialreturn/figure/Fig_A13B_cite+fednoit.png saved as PNG format

. restore

. 
. 
. 
. 
. 
. 
. 
. 
. log close
      name:  <unnamed>
       log:  /Users/gxzheng/Desktop/fintech/socialreturn/social_return.log
  log type:  text
 closed on:  16 May 2023, 17:43:47
------------------------------------------------------------------------------------------------------------------------
